package com.kaikai.commonutils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements ApplicationContextAware
{
    private static ApplicationContext applicationContext;
    
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }
    
    public static ApplicationContext getApplicationContext() {
        return SpringContextUtils.applicationContext;
    }
    
    public static Object getBean(final String name) {
        return getApplicationContext().getBean(name);
    }
    
    public static <T> T getBean(final Class<T> clazz) {
        return (T)getApplicationContext().getBean((Class)clazz);
    }
    
    public static <T> T getBean(final String name, final Class<T> clazz) {
        return (T)getApplicationContext().getBean(name, (Class)clazz);
    }
}
